{#
This file is part of EC-CUBE

Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.

http://www.ec-cube.co.jp/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#}
{% extends 'default_frame.twig' %}

{% set menus = ['product', 'product_master'] %}

{% block title %}商品管理{% endblock %}
{% block sub_title %}商品登録(商品規格){% endblock %}

{% if not_product_class  %}
    {% form_theme form 'Form/bootstrap_3_horizontal_layout.html.twig' %}
{% else %}
    {% form_theme classForm 'Form/bootstrap_3_horizontal_layout.html.twig' %}
{% endif %}

{% block javascript %}
<script>
    $(function() {

        // 無制限チェックボックス初期表示
        $('input[id$=_stock_unlimited]').each(function() {
            var check = $(this).prop('checked');
            var index = $(this).attr('id').match(/\d+/);

            if (check) {
                $('#form_product_classes_' + index +'_stock').prop('disabled', true);
            } else {
                $('#form_product_classes_' + index +'_stock').prop('disabled', false);
            }
        });



        // 無制限チェックボックス
        $('input[id$=_stock_unlimited]').click(function() {
            var check = $(this).prop('checked');
            var index = $(this).attr('id').match(/\d+/);

            if (check) {
                $('#form_product_classes_' + index +'_stock').prop('disabled', true);
            } else {
                $('#form_product_classes_' + index +'_stock').prop('disabled', false);
            }
        });

        // 登録チェックボックス
        $('#add-all').click(function() {
            var addall = $('#add-all').prop('checked');
            if (addall) {
                $('input[id$=_add]').prop('checked', true);
            } else {
                $('input[id$=_add]').prop('checked', false);
            }
        });

        // 1行目をコピーボタン
        $('#copy').click(function() {
            var check = $('#form_product_classes_0_add').prop('checked');
            $('input[id$=_add]').prop('checked', check);

            var product_code = $('#form_product_classes_0_code').val();
            $('input[id$=_code]').val(product_code);

            var stock = $('#form_product_classes_0_stock').val();
            $('input[id$=_stock]').val(stock);

            var stock_unlimited = $('#form_product_classes_0_stock_unlimited').prop('checked');
            // 無制限チェックボックス
            $('input[id$=_stock_unlimited]').each(function() {
                var index = $(this).attr('id').match(/\d+/);

                if (stock_unlimited) {
                    $(this).prop('checked', true);
                    $('#form_product_classes_' + index +'_stock').prop('disabled', true);
                } else {
                    $(this).prop('checked', false);
                    $('#form_product_classes_' + index +'_stock').prop('disabled', false);
                }
            });

            var sale_limit = $('#form_product_classes_0_sale_limit').val();
            $('input[id$=_sale_limit]').val(sale_limit);

            var price01 = $('#form_product_classes_0_price01').val();
            $('input[id$=_price01]').val(price01);

            var price02 = $('#form_product_classes_0_price02').val();
            $('input[id$=_price02]').val(price02);

            var delivery_fee = $('#form_product_classes_0_delivery_fee').val();
            $('input[id$=_delivery_fee]').val(delivery_fee);

            var delivery_date = $('#form_product_classes_0_delivery_date').val();
            $('select[id$=_delivery_date]').val(delivery_date);

            var tax_rate = $('#form_product_classes_0_tax_rate').val();
            $('input[id$=_tax_rate]').val(tax_rate);

            var product_type = $('#form_product_classes_0_product_type_1').prop('checked');
            if (product_type) {
                $('input[id$=_product_type_1]').prop('checked', true);
            } else {
                $('input[id$=_product_type_2]').prop('checked', true);
            }

        });


        $('#delete').click(function() {
            if (confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか？')) {
                $('#mode').val('delete');
                $('#product-class-form').submit();
                return true;
            }
            return false;
        });


    });
</script>
{% endblock javascript %}


{% block main %}
<div id="edit_info_wrap" class="row">
    <div id="edit_info_box" class="col-md-12">
        <form class="form-inline" method="post" action="{{ url('admin_product_product_class', {id : Product.id}) }}">
            <div id="edit_info_box__body" class="box">
                <div id="edit_info_box__header" class="box-header">
                    商品名 : <h3 class="box-title">{{ Product.name }}</h3>
                </div><!-- /.box-header -->
                <div id="edit_info_box__body" class="box-body" style="padding-bottom: 30px;">
                    {% if not_product_class  %}
                        {{ form_widget(form._token) }}
                        <button type="submit" class="btn btn-primary pull-right">商品規格の設定</button>
                        <div id="edit_info_box__class_name" class="form-horizontal">
                            {{ form_widget(form.class_name1) }}
                            {{ form_errors(form.class_name1) }}
                            {{ form_widget(form.class_name2) }}
                            {{ form_errors(form.class_name2) }}

                        </div>
                        <div class="extra-form form-inline row">
                            {% for f in form.getIterator %}
                                {% if f.vars.name matches '[^plg*]' %}
                                    <div class="col-sm-12 form-group">
                                    {{ form_label(f) }}
                                    {{ form_widget(f) }}
                                    {{ form_errors(f) }}
                                    </div>
                                {% endif %}
                            {% endfor %}
                        </div>
                    {% else %}
                        <button type="button" id="delete" class="btn btn-default pull-right" name="mode" value="delete">商品規格を初期化</button>
                        <div id="edit_info_box__class_name">
                          規格1 : <strong>{{ class_name1 }}</strong>
                          {% if class_name2 is not null %}
                          <br>規格2 : <strong>{{ class_name2 }}</strong>
                          {% endif %}
                        </div>
                    {% endif %}
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </form>

    </div><!-- /.col -->
</div>


{% if classForm is not null %}
<form id="product-class-form" class="form-inline" method="post" action="{{ url('admin_product_product_class_edit', { id : Product.id}) }}">
{{ form_widget(classForm._token) }}
<div id="result_wrap" class="row">
    <div id="result_box" class="col-md-12">
        <div id="result_box__body" class="box">
            {% if classForm.product_classes|length > 0 and has_class_category_flg %}
            <div id="result_box__header" class="box-header">
                <button type="button" id="copy" class="btn btn-default pull-right btn-xs">1行目のデータをコピーする</button>
                <h3 class="box-title">検索結果 <span class="normal"><strong>{{ classForm.product_classes|length }} 件</strong> が該当しました</span></h3>
                {% if error is not null %}
                    <div id="result_box__error" class="text-danger">{{ error.message }}</div>
                {% endif %}
            </div><!-- /.box-header -->
            <div id="result_box__body_inner" class="box-body no-padding">
                <div id="result_box__list_box" class="table_list">
                    <div id="result_box__list" class="table-responsive with-border table-menu table-responsive-overflow">
                        <table class="table table-striped">
                            <thead>
                                <tr id="result_box__header">
                                    <th id="result_box__header_add" class="text-center">登録<input id="add-all" type="checkbox" value="0"></th>
                                    <th id="result_box__header_class_category1">規格1</th>
                                    <th id="result_box__header_class_category2">規格2</th>
                                    <th id="result_box__header_code">商品コード</th>
                                    <th id="result_box__header_stock">在庫数</th>
                                    <th id="result_box__header_sale_limit">販売制限数</th>
                                    <th id="result_box__header_price01">通常価格(円)</th>
                                    <th id="result_box__header_price02">販売価格(円)</th>
                                    {% if BaseInfo.option_product_delivery_fee %}
                                    <th id="result_box__header_delivery_fee">送料</th>
                                    {% endif %}
                                    <th id="result_box__header_delivery_date">お届け可能日</th>
                                    {% if BaseInfo.option_product_tax_rule %}
                                    <th id="result_box__header_tax_rate">販売税率</th>
                                    {% endif %}
                                    <th id="result_box__header_product_type">商品種別</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for product_class_form in classForm.product_classes %}
                            <tr  id="result_box__item--{{ product_class_form.vars.name }}">
                                <td id="result_box__add--{{ product_class_form.vars.name }}" class="text-center">
                                    {{ form_widget(product_class_form.add) }}
                                </td>
                                <td id="result_box__class_category1--{{ product_class_form.vars.name }}">
                                    {{ product_class_form.vars.value.ClassCategory1 }}
                                    {{ form_widget(product_class_form.ClassCategory1) }}
                                </td>
                                <td id="result_box__class_category2--{{ product_class_form.vars.name }}">
                                    {{ product_class_form.vars.value.ClassCategory2 }}
                                    {{ form_widget(product_class_form.ClassCategory2) }}
                                </td>
                                <td id="result_box__code--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.code) }}
                                    {{ form_errors(product_class_form.code) }}
                                </td>
                                <td id="result_box__stock--{{ product_class_form.vars.name }}">
                                    {% if  product_class_form.vars.value.stock_unlimited %}
                                    {# 在庫無制限時のdisable #}
                                    {% endif %}
                                    {{ form_widget(product_class_form.stock) }}
                                    {{ form_errors(product_class_form.stock) }}
                                    {{ form_widget(product_class_form.stock_unlimited) }}
                                    {{ form_errors(product_class_form.stock_unlimited) }}
                                </td>
                                <td id="result_box__sale_limit--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.sale_limit) }}
                                    {{ form_errors(product_class_form.sale_limit) }}
                                </td>
                                <td id="result_box__price01--{{ product_class_form.vars.name }}" class="price_cell">
                                    {{ form_widget(product_class_form.price01, {'attr': {'class': 'notmoney'}}) }}
                                    {{ form_errors(product_class_form.price01) }}
                                </td>
                                <td id="result_box__price02--{{ product_class_form.vars.name }}" class="price_cell">
                                    {{ form_widget(product_class_form.price02, {'attr': {'class': 'notmoney'}}) }}
                                    {{ form_errors(product_class_form.price02) }}
                                </td>
                                {% if BaseInfo.option_product_delivery_fee %}
                                <td id="result_box__delivery_fee--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.delivery_fee, {'attr': {'class': 'notmoney'}}) }}
                                    {{ form_errors(product_class_form.delivery_fee) }}
                                </td>
                                {% endif %}
                                <td id="result_box__delivery_date--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.delivery_date) }}
                                    {{ form_errors(product_class_form.delivery_date) }}
                                </td>
                                {% if BaseInfo.option_product_tax_rule %}
                                <td id="result_box__tax_rate--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.tax_rate) }}
                                    {{ form_errors(product_class_form.tax_rate) }}
                                </td>
                                {% endif %}
                                <td id="result_box__product_type--{{ product_class_form.vars.name }}">
                                    {{ form_widget(product_class_form.product_type) }}
                                    {{ form_errors(product_class_form.product_type) }}
                                </td>
                            </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div><!-- /.box-body -->
            {% else %}
            <div id="result_box__error_message" class="box-header">
                <h3 class="box-title">検索条件に該当するデータがありませんでした。</h3>
            </div><!-- /.box-header -->
            {% endif %}
        </div><!-- /.box -->
    </div><!-- /.col -->
</div>

{% if classForm.product_classes|length > 0 and has_class_category_flg %}
<div id="edit_box__footer" class="row">
    <div id="edit_box__button_menu" class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-3 text-center btn_area">
{% if not_product_class  %}
        <button type="submit" class="btn btn-primary btn-lg btn-block" name="mode" value="edit">登録</button>
{% else %}
        <input id="mode" type="hidden" name="mode">
        <button type="submit" class="btn btn-primary btn-lg btn-block" name="mode" value="update">更新</button>
{% endif %}
        <p><a href="{{ url('admin_product_product_edit', {id : Product.id}) }}">商品登録に戻る</a></p>
    </div>
</div>
</form>
{% endif %}

{% endif %}



{% endblock %}
